<?php
include "../../db/dbconnect.php";


$query = "SELECT * FROM User";
if(isset($_GET['sort'])){
	$query .= " ORDER BY {$_GET['sort']} DESC";
}else{
	$query .= " ORDER BY gold DESC"; // default
}
$query .= " LIMIT 10";

$result = mysql_query($query);
if($result) $cnt = mysql_num_rows($result);
else $cnt = 0;

if(!isset($_GET['sort'])){
	generate_rank($result,$cnt);
}else{
	// Construct XML tree.
	$xml = new DOMDocument();
	$xml->encoding = "UTF-8";
	$xml->formatOutput = true;
	
	$ranking = $xml->appendChild($xml->createElement('ranking'));
	
	if($cnt > 0){
		while($row = mysql_fetch_array($result))
	    {
	    	$temp_level = $row['level'];
	    	if(empty($temp_level)) $temp_level = 1;
	    	$temp_gold = $row['gold'];
	    	if(empty($temp_gold)) $temp_gold = 0;
	    	$temp_exp = $row['exp'];
	    	if(empty($temp_exp)) $temp_exp=0;
	    	$temp_qcreated = $row['qcreated'];
	    	if(empty($temp_qcreated)) $temp_qcreated=0;
	    	$temp_bugkills = $row['bugkills'];
	    	if(empty($temp_bugkills)) $temp_bugkills=0;
	    	
	    	$rank = $ranking->appendChild($xml->createElement('rank'));
	    	$uid = $rank->appendChild($xml->createElement('uid'));
	    	$uid->appendChild($xml->createTextNode($row['uid']));
	    	$name = $rank->appendChild($xml->createElement('name'));
	    	$name->appendChild($xml->createTextNode($row['username']));
	    	$level = $rank->appendChild($xml->createElement('level'));
	    	$level->appendChild($xml->createTextNode($temp_level));
	    	$gold = $rank->appendChild($xml->createElement('gold'));
	    	$gold->appendChild($xml->createTextNode($temp_gold));
	    	$exp = $rank->appendChild($xml->createElement('exp'));
	    	$exp->appendChild($xml->createTextNode($temp_exp));
	    	$qcreated = $rank->appendChild($xml->createElement('qcreated'));
	    	$qcreated->appendChild($xml->createTextNode($temp_qcreated));
	    	$bugkills = $rank->appendChild($xml->createElement('bugkills'));
	    	$bugkills->appendChild($xml->createTextNode($temp_bugkills));
	    }
	}
	header("Content-type: text/xml");
	echo $xml->saveXML();
}
?>

<?php
function generate_rank($result,$cnt){
	if(isset($_SESSION['userinfo'])){
		$unserializedUser = unserialize($_SESSION['userinfo']);
		$user_id = $unserializedUser->getUid();
	}else{
		$user_id = 0;
	}
	?>
	<table id="ranking_table">
    	<tr>
    	    <th width="3%"></th>
    	    <th width="15%">Name</th>
    	    <th width="5%">Level</th>
    	    <th width="10%">Gold</th>
    	    <th width="10%">Exp</th>
    	    <th width="8%">Question</th>
    	    <th width="8%">BugKill</th>
    	</tr>
		<?php
		$rank = 1;
		if($cnt > 0){
			while($row = mysql_fetch_array($result)){
				if($row['uid'] == $user_id){
					$self = " class=\"self_user\"";
				}else{
					$self = "";
				}
				
				$temp_level = $row['level'];
		    	if(empty($temp_level)) $temp_level = 1;
		    	$temp_gold = $row['gold'];
		    	if(empty($temp_gold)) $temp_gold = 0;
		    	$temp_exp = $row['exp'];
		    	if(empty($temp_exp)) $temp_exp=0;
		    	$temp_qcreated = $row['qcreated'];
		    	if(empty($temp_qcreated)) $temp_qcreated=0;
		    	$temp_bugkills = $row['bugkills'];
		    	if(empty($temp_bugkills)) $temp_bugkills=0;
				?>
				<tr>
					<td<?php echo($self);?>><?php echo($rank);?></td>
					<td<?php echo($self);?>><?php echo($row['username']);?></td>
					<td<?php echo($self);?>><?php echo($temp_level);?></td>
					<td<?php echo($self);?>><?php echo($temp_gold);?></td>
					<td<?php echo($self);?>><?php echo($temp_exp);?></td>
					<td<?php echo($self);?>><?php echo($temp_qcreated);?></td>
					<td<?php echo($self);?>><?php echo($temp_bugkills);?></td>
				</tr>
			<?php
				$rank++;
			}
		}
		?>
	</table>
<?php
}
?>